Package vg.userInterface.scaling.components

Source Code of vg.userInterface.scaling.components.ZoomIn

package vg.userInterface.scaling.components;

import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;

import vg.core.IGraphView;

/**
* This class realizes zoom in.
* @author tzolotuhin
*/
public class ZoomIn {
  // Main components
  private final JButton button;
  // Data
  private IGraphView view;
  // Mutex
  private final Object theMutexObject;
  /**
   * Constructor.
   */
  public ZoomIn() {
    // init mutex
    this.theMutexObject = new Object();
    // init components
    this.button = new JButton(new ImageIcon("./data/resources/textures/plusButton.png"));
    this.button.setToolTipText("Zoom in");
    this.button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        zoomIn();
      }
    });
    this.button.setEnabled(false);
    // ctrl + plus
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {         
      public boolean dispatchKeyEvent(KeyEvent e) {
        // if use VK_PLUS, that it's will not work, because JDK has bug with it.
        if (e.getID() == KeyEvent.KEY_PRESSED && (e.getKeyChar() == '+' || e.getKeyCode() == KeyEvent.VK_EQUALS) && e.isControlDown()) {
          zoomIn();
          return(true);
        }
        return(false);
      }
    });
  }
  public JComponent getView() {
    return(this.button);
  }
  /**
   * This method changes current view.
   */
  public void changeView(final IGraphView newView) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        synchronized (ZoomIn.this.theMutexObject) {
          ZoomIn.this.view = newView;
          button.setEnabled(ZoomIn.this.view != null);
        }
      }
    });
  }
  ///////////////////////////////////////////////////////////////////////////
  // PRIVATE METHOD
  ///////////////////////////////////////////////////////////////////////////
  private void zoomIn() {
    synchronized (this.theMutexObject) {
      if(this.view!=null) {
        this.view.zoomIn();
      }
    }
  }
}
TOP

Related Classes of vg.userInterface.scaling.components.ZoomIn

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.